package ch.ethz.inf.shop.model;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;

import ch.ethz.inf.shop.Title;
import ch.ethz.inf.shop.model.simpleDatabase.Tuple;

public class RecipeItem {

	@Title(text = "Recipe Item ID", isVisible = false)
	final private int recipeItemId;

	@Title(text = "Product", isVisible = true)
	final private Product product;

	@Title(text = "Recipe", isVisible = false)
	final private Recipe recipe;

	@Title(text = "Amount", isVisible = true)
	final private int amount;

	@Title(text = "Price", isVisible = true)
	final private BigDecimal linePrice;

	public RecipeItem(int recipeItemId, Product product, Recipe recipe, int amount, BigDecimal linePrice) {
		super();
		this.recipeItemId = recipeItemId;
		this.product = product;
		this.recipe = recipe;
		this.amount = amount;
		this.linePrice = linePrice;
	}

	public RecipeItem(final ResultSet rs, Recipe recipe) throws SQLException {
		this.recipe = recipe;
		this.amount = rs.getInt("amount");
		this.recipeItemId = rs.getInt("recipe_item_id");
		this.product = new Product(rs);
		this.linePrice = rs.getBigDecimal("price");
	}

	public RecipeItem(final Tuple tuple, Recipe recipe) {
		this.recipe = recipe;
		this.amount = tuple.getInt("recipe_item.amount");
		this.recipeItemId = tuple.getInt("recipe_item.recipe_item_id");
		this.product = new Product(tuple);
		this.linePrice = tuple.getBigDecimal("recipe_item.price");
	}

	public int getRecipeItemId() {
		return recipeItemId;
	}

	public Product getProduct() {
		return product;
	}

	public Recipe getRecipe() {
		return recipe;
	}

	public int getAmount() {
		return amount;
	}

	public String getName() {
		return product.getName();
	}

	public int getProductId() {
		return product.getProductId();
	}

	public int getWeight() {
		return product.getWeight();
	}

	public String getCountryOfOrigin() {
		return product.getCountryOfOrigin();
	}

	public Category getCategory() {
		return product.getCategory();
	}

	public Brand getBrand() {
		return product.getBrand();
	}

	public BigDecimal getLinePrice() {
		return linePrice;

	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + amount;
		result = prime * result + ((linePrice == null) ? 0 : linePrice.hashCode());
		result = prime * result + ((product == null) ? 0 : product.hashCode());
		result = prime * result + ((recipe == null) ? 0 : recipe.hashCode());
		result = prime * result + recipeItemId;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		RecipeItem other = (RecipeItem) obj;
		if (amount != other.amount)
			return false;
		if (linePrice == null) {
			if (other.linePrice != null)
				return false;
		} else if (!linePrice.equals(other.linePrice))
			return false;
		if (product == null) {
			if (other.product != null)
				return false;
		} else if (!product.equals(other.product))
			return false;
		if (recipe == null) {
			if (other.recipe != null)
				return false;
		} else if (!recipe.equals(other.recipe))
			return false;
		if (recipeItemId != other.recipeItemId)
			return false;
		return true;
	}
}
